<?php 
//  vim:set fdm=indent ts=2 ai et sts=2 sw=2 tw=0:
//  simple database query class by $Author: anton $ 
//  $Date: 2008-02-26 11:31:53 +0800 (二, 26  2月 2008) $
//  dict.php
//  字典程式 功能定義 $URL
//  Released as a GPL licensed software.
define (STOCK_INTERVAL,300); // 5 min

function stock_data($no)
{
  $needtoget = false;
  $now = time();
  $now = ($now+TZSTAMP);
  if (!$no) $no="2382";

  $fst_existed=file_exists("temp/$no");
  if ($fst_existed) 
  {
    $fst = stat("temp/$no");
    $fct = $fst[ctime]; // file create time stamp
    if (($now - $fct) < STOCK_INTERVAL )
    {
      $needtoget = false;
    } else if (($now%86400)>48600) // 48600==13:30,32400==9:00 午後
    {
      // 如果同一天 就看 $fct 有沒有超過 13:30 
      // 有就不抓
      if ( (ceil($now/86400)==ceil($fct/86400)) && ($fct%86400)>48600 )
      {
        $needtoget = false;
      } else {
        $needtoget = true;
      }

    } else if (($now%86400)<32400) // 9:00 之前
    {
      // 同一天就不用抓了，看 $fct 有沒有大於 13:30 有的話
      if (ceil($now/86400)==ceil($fct/86400))
      {
        $needtoget = false;
      } else if (ceil($now/86400)-ceil($fct/86400)>=1)
      {
        // 超過一天也一定要抓
        $needtoget = true;
      } else if (($fct%86400)<48600) // 13:30 之前一定要抓
      {
        $needtoget = true;
      } else {
        $needtoget = false;
      }
    } else {
      $needtoget = true;
    }
  } else {
    $needtoget = true;
  }

  if ($needtoget)
  {
    $url="http://mis.tse.com.tw/data/$no.csv?ID=$now"."00";
    $fs=fopen($url,"r");
    $content="";
    if ($fs)
    while(!feof($fs))
    $content.=fread($fs,1024);
    fclose($fs);
    $content=iconv("Big5","UTF-8",$content);
    if (strstr("錯誤",$content)) return "錯誤的代號";
    $fs=fopen("temp/$no","w");
    fputs($fs,$content);
    fclose($fs);
  } else {
    $url="temp/$no";
    $fs=fopen($url,"r");
    $content="";
    while(!feof($fs))
    $content.=fread($fs,1024);
    fclose($fs);
  }
  $content=stripslashes($content);
  $cols=explode(",",$content);
  $chopstring=Array(" ","\"");
  for ($i=0;$i<count($cols);$i++)
  {
    $cols[$i]=str_replace($chopstring,"",$cols[$i]);
  }

  $cols[32]=rtrim($cols[32]); // name chop
  return $cols;
}

function stock_price($no)
{
  $data = stock_data($no);
  return $data[8]; // 成交價
}

function stock($no)
{
  $data = stock_data($no);
  $data[32]=rtrim($data[32]); // name chop
  if ($data[8]==$data[3]) 
    $mark="⊕";
  else if ($data[8]==$data[4])
    $mark="◒";
  else if ($data[1]>0)
    $mark="▲";
  else if ($data[1]<0)
    $mark="▼";
  else
    $mark="=";

  $ans = $data[32]."[".$data[0]."]
更新時間: $data[2]
成交價: $data[8] 元
當盤成交量: $data[10] 張
當日成交量: $data[9] 張
今日漲跌: $mark  $data[1] 元
今日最高價: $data[6] 元
今日最低價: $data[7] 元
委買量: $data[12] 張
委賣量: $data[22] 張
";
  $ans=str_replace("?","0",$ans);
  return $ans;
}

function jpdict($word)
{
  $word = iconv("UTF-8","EUCJP",$word);
  $url = "http://dictionary.goo.ne.jp/search.php?MT=".urlencode($word)."&kind=all&mode=0&kwassist=0";
  $content = file_get_contents($url);
  iconv_set_encoding("internal_encoding", "UTF-8");
  iconv_set_encoding("output_encoding", "UTF-8");
  $content = iconv("EUCJP","UTF-8",$content);
  $content = str_replace("\r","",$content);
  $content = str_replace("\n","",$content);
  $tag = "diclst1";
  preg_match_all("/<!--".$tag."-->(.*?)<!--\/".$tag."-->/",$content,$matches);
  $matches=$matches[1];
  $ans = "";
  for ($i=0;$i<count($matches);$i++)
  {
    $ans .= str_replace("&nbsp;"," ",strip_tags($matches[$i]))."\n";
  }

  return (($ans)?$ans:"查無結果】");
}

function dict($word) // stardict
{
  if (strstr($word," ")) return;
  if (strstr($word,";")) return;

  exec("/usr/bin/sdcv -n $word",$out);
  $ans="";
  $sum = count($out);
  for ($i=0;$i<$sum;$i++)
  {
    if (!strstr($out[$i],"save to cache"))
      $ans.=$out[$i]."\n";
  }
  exec("tool/tdic.sh $word",$out);
  $sum = count($out);
  for ($i=0;$i<$sum;$i++)
  {
    $ans.=$out[$i]."\n";
  }
  $ans = htmlspecialchars($ans); 
  return $ans;
}

function UNLiu($word)
{
  global $UNLiuline,$CangJieline,$Array30line,$ZhuYinline;

  $z_orig = Array (",","-",".","/","0","1","2","3","4","5","6","7","8","9",";","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
  $z_subst= Array ("ㄝ","ㄦ","ㄡ","ㄥ","ㄢ","ㄅ","ㄉ","ˇ","ˋ","ㄓ","ˊ","˙","ㄚ","ㄞ","ㄤ","ㄇ","ㄖ","ㄏ","ㄎ","ㄍ","ㄑ","ㄕ","ㄘ","ㄛ","ㄨ","ㄜ","ㄠ","ㄩ","ㄙ","ㄟ","ㄣ","ㄆ","ㄐ","ㄋ","ㄔ","ㄧ","ㄒ","ㄊ","ㄌ","ㄗ","ㄈ");
  $c_orig = Array ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
  $c_subst= Array ("日","月","金","木","水","火","土","竹","戈","十","大","中","一","弓","人","心","手","口","尸","廿","山","女","田","難","卜","重");
  $a_orig = Array (",",",",".",".","/","/","0","1","2","3","4","5","6","7","8","9",";",";","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
  $a_subst= Array("8v","8v","9v","9v","0v","0v","0","1","2","3","4","5","6","7","8","9","0-","0-","1-","5v","3v","3-","3^","4-","5-","6-","8^","7-","8-","9-","7v","6v","9^","0^","1^","4^","2-","5^","7^","4v","2^","2v","6^","1v");
  mb_internal_encoding("UTF-8");
  $len  = mb_strlen($word); 
  if ($len==0) return "請輸入 #no [查詢的字串]";
  $ans ="";

  $sum  = count($UNLiuline);
  if (is_numeric($word))
  {
    $ans.="unicode 十進位碼轉換後為: '".uni2utf8($word)."'\n";
    $ans.="unicode 十六進位碼轉換後為: '".uni2utf8(hexdec($word))."' \n";
  } else {
    for ($x=0;$x<$len;$x++)
    {
      $w = mb_substr($word,$x,1);
      $found = false;
      $ans.="[$w] unicode 是： ".utf2uni($w)." hex:".dechex(utf2uni($w))."\n";
      $ans.="[$w] 的蝦米碼是：\n";
      for ($i=0;$i<$sum-1;$i++)
      {
        $col = explode(" ",$UNLiuline[$i]);
        if ($col[1]==$w) 
        {
          $ans.=$col[0]." ";
          $found = true;
        }
      }
      if ($found)
        $ans.="\n";
      else
        $ans.="找不到合適的蝦米碼。\n";
    }
    $sum  = count($ZhuYinline);
    for ($x=0;$x<$len;$x++)
    {
      $w = mb_substr($word,$x,1);
      $found = false;
      $ans.="[$w] 的注音是：\n";
      for ($i=0;$i<$sum-1;$i++)
      {
        $col = explode(" ",$ZhuYinline[$i]);
        if ($col[1]==$w) 
        {
          $ans.=str_replace($z_orig,$z_subst,$col[0])." ";
          $found = true;
        }
      }
      if ($found)
        $ans.="\n";
      else
        $ans.="找不到合適的注音。\n";
    }

    $sum  = count($CangJieline);
    for ($x=0;$x<$len;$x++)
    {
      $w = mb_substr($word,$x,1);
      $found = false;
      $ans.="[$w] 的倉頡碼是：\n";
      for ($i=0;$i<$sum-1;$i++)
      {
        $col = explode(" ",$CangJieline[$i]);
        if ($col[1]==$w) 
        {
          $ans.=str_replace($c_orig,$c_subst,$col[0])." ";
          $found = true;
        }
      }
      if ($found)
        $ans.="\n";
      else
        $ans.="找不到合適的倉頡碼。\n";
    }

    $sum  = count($Array30line);
    for ($x=0;$x<$len;$x++)
    {
      $w = mb_substr($word,$x,1);
      $found = false;
      $ans.="[$w] 的行列碼是：\n";
      for ($i=0;$i<$sum-1;$i++)
      {
        $col = explode(" ",$Array30line[$i]);
        if ($col[1]==$w) 
        {
          $ans.=str_replace($a_orig,$a_subst,$col[0])." ";
          $found = true;
        }
      }
      if ($found)
        $ans.="\n";
      else
        $ans.="找不到合適的行列碼。\n";
    }
  } 
    return $ans;
}

function utf2uni($utf8_char)
{
   $ch=ord(substr($utf8_char,0,1));
   if ($ch<0x80) return $ch;
   if ($ch>0xBF && $ch<0xFE)
   {
      if ($ch<0xE0) {
         $i=1;
         $uni_code=$ch-0xC0;
      } elseif ($ch<0xF0)   {
         $i=2;
         $uni_code=$ch-0xE0;
      } elseif ($ch<0xF8)   {
         $i=3;
         $uni_code=$ch-0xF0;
      } elseif ($ch<0xFC)   {
         $i=4;
         $uni_code=$ch-0xF8;
      } else {
         $i=5;
         $uni_code=$ch-0xFC;
      }
   } else {
      echo "UTF-8 code error!!";exit;
   }
   
   for ($j=0;$j<$i;$j++)
   {
      $ch=ord(substr($utf8_char,$j+1,1))-0x80;
      $uni_code=$uni_code*64+$ch;
   }
   return $uni_code;
}

function uni2utf8($c)
{
    $str="";
    if ($c < 0x80) {
         $str.=$c;
    } else if ($c < 0x800) {
         $str.=chr(0xC0 | $c>>6);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c < 0x10000) {
         $str.=chr(0xE0 | $c>>12);
         $str.=chr(0x80 | $c>>6 & 0x3F);
         $str.=chr(0x80 | $c & 0x3F);
    } else if ($c < 0x200000) {                               
         $str.=chr(0xF0 | $c>>18);
         $str.=chr(0x80 | $c>>12 & 0x3F);
         $str.=chr(0x80 | $c>>6 & 0x3F);
         $str.=chr(0x80 | $c & 0x3F);
    }
    return $str;
}
?>
